package com.yys.huarongdao;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.yys.huarongdao.dao.MySQLiteOpenHelper;
import com.yys.huarongdao.entity.ArrayIcon;
import com.yys.huarongdao.entity.RankingItem;
import com.yys.huarongdao.fragment.RankingListFragment;

import java.util.ArrayList;

public class RankingListActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private Button rbut_3x3;
    private Button rbut_4x4;
    private Button rbut_5x5;
    private Fragment f1,f2,f3;
    private RadioGroup bar_bottom;

    private RankingListAdapter rankingListAdapter;
    private ArrayList<RankingItem> items_3x3,items_4x4,items_5x5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportActionBar()!=null){
            getSupportActionBar().hide();
        }
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_ranking_list);
        bindViews();
    }

    private void bindViews(){
        rbut_3x3=findViewById(R.id.rbut_3x3);
        rbut_4x4=findViewById(R.id.rbut_4x4);
        rbut_5x5=findViewById(R.id.rbut_5x5);
        bar_bottom=findViewById(R.id.bar_bottom);
        bar_bottom.setOnCheckedChangeListener(this);

        //首次默认打开3x3华容道排行榜
        FragmentManager fm=getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if(f1==null){
            if(items_3x3==null)
                items_3x3= getRankingList(3);
            f1=new RankingListFragment(new RankingListAdapter(items_3x3,this),"3x3华容道排行榜");
        }
        ft.replace(R.id.fragment,f1);
        ft.commit();

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentManager fm=getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        switch (checkedId){
            case R.id.rbut_3x3:
                if(f1==null){
                    if(items_3x3==null)
                        items_3x3= getRankingList(3);
                    f1=new RankingListFragment(new RankingListAdapter(items_3x3,this),"3x3华容道排行榜");
                }
                ft.replace(R.id.fragment,f1);
                break;
            case R.id.rbut_4x4:
                if(f2==null){
                    if(items_4x4==null)
                        items_4x4= getRankingList(4);
                    f2=new RankingListFragment(new RankingListAdapter(items_4x4,this),"4x4华容道排行榜");
                }
                ft.replace(R.id.fragment,f2);
                break;
            case R.id.rbut_5x5:
                if(f3==null){
                    if(items_5x5==null)
                        items_5x5= getRankingList(5);
                    f3=new RankingListFragment(new RankingListAdapter(items_5x5,this),"5x5华容道排行榜");
                }
                ft.replace(R.id.fragment,f3);
                break;
        }
        ft.commit();
    }

    /**
     * 获取排行榜
     *
     */
    public ArrayList<RankingItem> getRankingList(int rank_type){
        ArrayList<RankingItem> items=new ArrayList<RankingItem>();
        MySQLiteOpenHelper sqLiteOpenHelper=new MySQLiteOpenHelper(this,"huarongdao.db",null,1);
        SQLiteDatabase database=sqLiteOpenHelper.getReadableDatabase();
        Cursor cursor=database.rawQuery("select account,time_spent,date from ranking_list,user where user.userid=ranking_list.userid and rank_type=? order by time_spent asc,date asc",new String[]{String.valueOf(rank_type)});
        String account,time_spent,date;

        int index=0;
        if(cursor.moveToFirst()){
            do{
                account=cursor.getString(0);
                time_spent=cursor.getString(1);
                date=cursor.getString(2);
                items.add(new RankingItem(ArrayIcon.icons[index],account, time_spent, date));
                index++;
            }while (cursor.moveToNext());
        }
        return items;
    }

    /**
     * 排行榜List的适配器
     */
    public class RankingListAdapter extends BaseAdapter {

        private ArrayList<RankingItem> items;
        private Context mContext;

        public RankingListAdapter(ArrayList<RankingItem> items, Context context) {
            this.items = items;
            this.mContext=context;
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @RequiresApi(api = Build.VERSION_CODES.N)
        @SuppressLint("ResourceType")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Log.d("BaseAdapter","convertView"+convertView+",position"+position);

            ViewHolder holder=null;
            if (convertView == null){
                convertView= LayoutInflater.from(mContext).inflate(R.layout.list_item_ranking,parent,false);
                holder=new ViewHolder();
                holder.img=convertView.findViewById(R.id.list_item_ranking_img);
                holder.account=convertView.findViewById(R.id.list_item_ranking_tv_account);
                holder.time_spent=convertView.findViewById(R.id.list_item_ranking_tv_time_spent);
                holder.date=convertView.findViewById(R.id.list_item_ranking_tv_date);
                holder.layout=convertView.findViewById(R.id.list_item_ranking);
                convertView.setTag(holder);
            }else {
                holder=(ViewHolder)convertView.getTag();
            }
            holder.img.setImageResource(items.get(position).getImg());
            holder.account.setText(items.get(position).getAccount());
            holder.time_spent.setText(items.get(position).getTime_spent());
            SimpleDateFormat sdf = new SimpleDateFormat(new String("yyyy年MM月dd日 HH:mm:ss"));
            holder.date.setText(sdf.format(Long.parseLong(items.get(position).getDate())));
            if(position%2==0){
                holder.layout.setBackgroundResource(R.drawable.listview_item_selector);
            }else {
                holder.layout.setBackgroundResource(R.drawable.listview_item_white);
            }
            
            return convertView;
        }

        class ViewHolder{
            ImageView img;
            TextView account;
            TextView time_spent;
            TextView date;
            LinearLayout layout;
        }
    }
}
